const app = getApp()
import {
  addUserDeliveryAddress,
  editUserDeliveryAddress
} from "../../common/api";
Page({
  data: {
    checked: false,
    deliveryRange: "",
    contactPhone: "",
    contactName: "",
    address: "",
    id: undefined
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onPullDownRefresh: function () {
    this.onRefresh();
  },
  onRefresh: function () {
  },
  onLoad: function (options) {
    console.log(options)
    this.options = options
    this.setData({
      deliveryRange: options.deliveryRange,
      contactPhone: options.contactPhone !== undefined ? options.contactPhone : "",
      contactName: options.contactName !== undefined ? options.contactName : "",
      address: options.address !== undefined ? options.address : "",
      id: options.id
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   },
  saveAddress: function (e) {
    var contactName = e.detail.value.contactName;
    var contactPhone = e.detail.value.contactPhone;
    var address = e.detail.value.address;
    if (contactName == '') {
      wx.showToast({
        title: '请输入收货人',
        icon: 'error'
      })
      return
    }
    else if (contactPhone == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'error'
      })
      return
    }
    else if (address == '') {
      wx.showToast({
        title: '请输入详细地址',
        icon: 'error'
      })
      return
    }
    else if (contactPhone.length != 11) {
      wx.showToast({
        title: '手机号格式错误',
        icon: 'error'
      })
      return
    }
    let id = this.data.id;
    if (id == undefined) {
      let data = {
        contactPhone: contactPhone,
        contactName: contactName,
        address: address
      }
      wx.showLoading({
        title: '保存中',
        mask: true
      })
      addUserDeliveryAddress(data).then(res => {
        wx.hideLoading();
        if (res.code == 200) {
          wx.showToast({
            title: '添加成功',
            icon: 'success'
          })
          var pages = getCurrentPages();
          var prevPage = pages[pages.length - 2]  //上一个页面
          prevPage.getAddressList()
          wx.navigateBack({
            delta: 1,
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
        }
      }).catch(err => {
        wx.hideLoading();
        wx.showToast({
          title: "保存失败",
          icon: 'error'
        })
      })
    } else {
      let data = {
        contactPhone: contactPhone,
        contactName: contactName,
        address: address,
        id: this.data.id
      }
      wx.showLoading({
        title: '修改中',
        mask: true
      })
      editUserDeliveryAddress(data).then(res => {
        wx.hideLoading();
        if (res.code == 200) {
          wx.showToast({
            title: '修改成功',
            icon: 'success'
          })
          var pages = getCurrentPages();
          var prevPage = pages[pages.length - 2]  //上一个页面
          prevPage.getAddressList()
          wx.navigateBack({
            delta: 1,
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
        }
      }).catch(err => {
        wx.hideLoading();
        wx.showToast({
          title: "修改失败",
          icon: 'error'
        })
      })
    }

  }
})